home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / hity wydania / Ubuntu 9.10 PL / karmelkowy-koliberek-9.10-netbook-remix-PL.iso / casper / filesystem.squashfs / usr / share / pyshared / invest / widgets.py < prev   
Text File  |  2009-10-20  |  7KB  |  224 lines

  1. import os, time
  2. from os.path import *
  3. import gnomeapplet, gtk, gtk.gdk, gconf, gobject, pango
  4. from gettext import gettext as _
  5. import gtk, gobject
  6. import csv, os
  7. from gettext import gettext as _
  8. import invest, invest.about, invest.chart
  9. from invest import *
  10.  
  11. COLORSCALE_POSITIVE = [
  12.     "white",
  13.     "#ad7fa8",
  14.     "#75507b",
  15.     "#5c3566",
  16.     "#729fcf",
  17.     "#3465a4",
  18.     "#204a87",
  19.     "#8ae234",
  20.     "#73d216",
  21.     "#4e9a06",
  22. ]
  23. GREEN = COLORSCALE_POSITIVE[-1]
  24. COLORSCALE_NEGATIVE = [
  25.     "white",
  26.     "#fce94f",
  27.     "#e9b96e",
  28.     "#fcaf3e",
  29.     "#c17d11",
  30.     "#f57900",
  31.     "#ce5c00",
  32.     "#ef2929",
  33.     "#cc0000",
  34.     "#a40000",
  35. ]
  36. RED = COLORSCALE_NEGATIVE[-1]
  37.  
  38. TICKER_TIMEOUT = 10000#3*60*1000#
  39.  
  40. class InvestWidget(gtk.TreeView):
  41.     def __init__(self, quotes_updater):
  42.         gtk.TreeView.__init__(self)
  43.         self.set_property("rules-hint", True)
  44.         self.set_reorderable(True)
  45.         self.set_hover_selection(True)
  46.  
  47.         simple_quotes_only = quotes_updater.simple_quotes_only()
  48.  
  49.         # model: SYMBOL, LABEL, TICKER_ONLY, BALANCE, BALANCE_PCT, VALUE, VARIATION_PCT, PB
  50.         # Translators: these words all refer to a stock. Last is short
  51.         # for "last price". Gain is referring to the gain since the
  52.         # stock was purchased.
  53.         col_names = [_('Ticker'), _('Last'), _('Change %'), _('Chart'), _('Gain'), _('Gain %')]
  54.         col_cellgetdata_functions = [self._getcelldata_label, self._getcelldata_value,
  55.             self._getcelldata_variation, None, self._getcelldata_balance, 
  56.             self._getcelldata_balancepct]
  57.         for i, col_name in enumerate(col_names):
  58.             if i < 3:
  59.                 cell = gtk.CellRendererText()
  60.                 column = gtk.TreeViewColumn (col_name, cell)
  61.                 if i == 0:
  62.                     column.set_sort_column_id(quotes_updater.LABEL)
  63.                 elif i == 2:
  64.                     column.set_sort_column_id(quotes_updater.VARIATION_PCT)
  65.                 column.set_cell_data_func(cell, col_cellgetdata_functions[i])
  66.                 self.append_column(column)
  67.             elif i == 3:
  68.                 cell_pb = gtk.CellRendererPixbuf()
  69.                 column = gtk.TreeViewColumn (col_name, cell_pb, pixbuf=quotes_updater.PB)
  70.                 self.append_column(column)
  71.             else:
  72.                 # add the last two column only if we have any positions
  73.                 if simple_quotes_only == False:
  74.                     cell = gtk.CellRendererText()
  75.                     column = gtk.TreeViewColumn (col_name, cell)
  76.                     if i == 4:
  77.                         column.set_sort_column_id(quotes_updater.BALANCE)
  78.                     elif i == 5:
  79.                         column.set_sort_column_id(quotes_updater.BALANCE_PCT)
  80.                     column.set_cell_data_func(cell, col_cellgetdata_functions[i])
  81.                     self.append_column(column)
  82.  
  83.         if simple_quotes_only == True:
  84.             self.set_property('headers-visible', False)
  85.  
  86.         self.connect('row-activated', self.on_row_activated)
  87.         self.set_model(quotes_updater)
  88.  
  89.     def _getcelldata_label(self, column, cell, model, iter):
  90.         cell.set_property('text', model[iter][model.LABEL])
  91.  
  92.     def _getcelldata_value(self, column, cell, model, iter):
  93.         cell.set_property('text', "%.5g" % model[iter][model.VALUE])
  94.  
  95.     def _getcelldata_variation(self, column, cell, model, iter):
  96.         color = GREEN
  97.         if model[iter][model.VARIATION_PCT] < 0: color = RED
  98.         change_pct = model[iter][model.VARIATION_PCT]
  99.         cell.set_property('markup',
  100.             "<span foreground='%s'>%+.2f%%</span>" %
  101.             (color, change_pct))
  102.  
  103.     def _getcelldata_balance(self, column, cell, model, iter):
  104.         is_ticker_only = model[iter][model.TICKER_ONLY]
  105.         color = GREEN
  106.         if model[iter][model.BALANCE] < 0: color = RED
  107.         if is_ticker_only:
  108.             cell.set_property('text', '')
  109.         else:
  110.                     cell.set_property('markup',
  111.             "<span foreground='%s'>%+.2f</span>" %
  112.             (color, model[iter][model.BALANCE]))
  113.  
  114.     def _getcelldata_balancepct(self, column, cell, model, iter):
  115.         is_ticker_only = model[iter][model.TICKER_ONLY]
  116.         color = GREEN
  117.         if model[iter][model.BALANCE] < 0: color = RED
  118.         if is_ticker_only:
  119.             cell.set_property('text', '')
  120.         else:
  121.                     cell.set_property('markup',
  122.             "<span foreground='%s'>%+.2f%%</span>" %
  123.             (color, model[iter][model.BALANCE_PCT]))
  124.  
  125.     def on_row_activated(self, treeview, path, view_column):
  126.         ticker = self.get_model()[self.get_model().get_iter(path)][0]
  127.         if ticker == None:
  128.             return
  129.  
  130.         invest.chart.show_chart([ticker])
  131.  
  132. #class InvestTicker(gtk.Label):
  133. #    def __init__(self):
  134. #        gtk.Label.__init__(self, _("Waiting..."))
  135. #
  136. #        self.quotes = []
  137. #        gobject.timeout_add(TICKER_TIMEOUT, self.scroll_quotes)
  138. #
  139. #        get_quotes_updater().connect('quotes-updated', self.on_quotes_update)
  140. #
  141. #    def on_quotes_update(self, updater):
  142. #        self.quotes = []
  143. #        updater.foreach(self.update_quote, None)
  144. #
  145. #    def update_quote(self, model, path, iter, user_data):
  146. #        color = GREEN
  147. #        if model[iter][model.BALANCE] < 0:
  148. #            color = RED
  149. #
  150. #        self.quotes.append(
  151. #            "%s: <span foreground='%s'>%+.2f (%+.2f%%)</span> %.2f" %
  152. #            (model[iter][model.SYMBOL], color, model[iter][model.BALANCE], model[iter][model.BALANCE_PCT], model[iter][model.VALUE]))
  153. #
  154. #    def scroll_quotes(self):
  155. #        if len(self.quotes) == 0:
  156. #            return True
  157. #
  158. #        q = self.quotes.pop()
  159. #        self.set_markup("<span face='Monospace'>%s</span>" % q)
  160. #        self.quotes.insert(0, q)
  161. #
  162. #        return True
  163. #
  164. #gobject.type_register(InvestTicker)
  165.  
  166. class InvestTrend(gtk.Image):
  167.     def __init__(self):
  168.         gtk.Image.__init__(self)
  169.         self.pixbuf = None
  170.         self.previous_allocation = (0,0)
  171.         self.connect('size-allocate', self.on_size_allocate)
  172.         get_quotes_updater().connect('quotes-updated', self.on_quotes_update)
  173.  
  174.     def on_size_allocate(self, widget, allocation):
  175.         if self.previous_allocation == (allocation.width, allocation.height):
  176.             return
  177.  
  178.         self.pixbuf = gtk.gdk.Pixbuf(gtk.gdk.COLORSPACE_RGB, True, 8, allocation.height, allocation.height)
  179.         self.set_color("grey")
  180.         self.previous_allocation = (allocation.width, allocation.height)
  181.  
  182.     def set_color(self, color, opacity=0xFF):
  183.         if self.pixbuf != None:
  184.             try:
  185.                 color = pango.Color(color)
  186.                 factor = float(0xFF)/0xFFFF
  187.                 self.pixbuf.fill(
  188.                     int(color.red*factor)<<24|int(color.green*factor)<<16|int(color.blue*factor)<<8|opacity)
  189.                 self.set_from_pixbuf(self.pixbuf)
  190.             except Exception, msg:
  191.                 invest.error("Could not set color: %s" % msg)
  192.  
  193.     def on_quotes_update(self, updater):
  194.         start_total = 0
  195.         now_total = 0
  196.         for row in updater:
  197.             # Don't count the ticker only symbols in the color-trend
  198.             if row[updater.TICKER_ONLY]:
  199.                 continue
  200.  
  201.             var = row[updater.VARIATION]/100
  202.             now = row[updater.VALUE]
  203.  
  204.             start = now / (1 + var)
  205.  
  206.             portfolio_number = sum([purchase["amount"] for purchase in invest.STOCKS[row[updater.SYMBOL]]["purchases"]])
  207.             start_total += start * portfolio_number
  208.             now_total += now * portfolio_number
  209.  
  210.         day_var = 0
  211.         if start_total != 0:
  212.             day_var = (now_total - start_total) / start_total * 100
  213.  
  214.         color = int(2*day_var)
  215.         opacity = min(0xFF, int(abs(127.5*day_var)))
  216.         if day_var < 0:
  217.             color = COLORSCALE_NEGATIVE[min(len(COLORSCALE_NEGATIVE)-1, abs(color))]
  218.         else:
  219.             color = COLORSCALE_POSITIVE[min(len(COLORSCALE_POSITIVE)-1, abs(color))]
  220.  
  221.         self.set_color(color, opacity)
  222.  
  223. gobject.type_register(InvestTrend)
  224.